package com.basswold.nasaesa.client;

import com.google.gwt.core.client.JavaScriptObject;

import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;



public class Weather {
	
	  public void getResponse(){
		String url = "http://api.wunderground.com/api/a4e7563412689635/conditions/q/55811.json?"; 
		url = URL.encode(url);
	
		final JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

		jsonp.setCallbackParam("callback");
		jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>() {
		   
			public void onFailure(final Throwable caught) {
		        Window.alert("JSONP onFailure");
		    }

		    public void onSuccess(JavaScriptObject s) {
		    JSONObject obj = new JSONObject(s);
		    String result = obj.toString();
		    JSONObject jA = (JSONObject)JSONParser.parseLenient(result);
		    JSONValue jTry = jA.get("current_observation");
		    
		    JSONObject jB = (JSONObject)JSONParser.parseLenient(jTry.toString());
		    JSONValue temp = jB.get("temp_c");
		    JSONValue vis = jB.get("visibility_km");
		    
		    String sTemp = temp.toString();
		    String sVis = vis.toString();
		    Window.alert("Temperature: " + sTemp + " " +
		    		"Visibility: " + sVis +"km");
		    }
		   
		});
}
}		
		